package com.itcast.cn.jsp.service;

import com.itcast.cn.jsp.mapper.BrandMapper;
import com.itcast.cn.jsp.pojo.Brand;
import com.itcast.cn.login.util.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

/**
 * <p>类描述</p>
 *
 * @author hyatt 2022/1/21 15:05
 * @version 1.0
 */
public class BrandService {
    private SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtil.getSqlSessionFactory();
    /**
     * 业务层调用数据访问层
     * @return
     */
    public List<Brand> selectAll() {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        // 调用方法
        List<Brand> brands = brandMapper.selectAll();
        sqlSession.close();
        return brands;
    }

    public void insert(Brand brand) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        // 调用方法
        brandMapper.insert(brand);
        // 提交
        sqlSession.commit();
        // 释放
        sqlSession.close();
    }

    public void update(Brand brand) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        // 调用方法
        brandMapper.update(brand);
        // 提交
        sqlSession.commit();
        // 释放
        sqlSession.close();
    }

    public void delete(int id) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        // 调用方法
        brandMapper.delete(id);
        // 提交
        sqlSession.commit();
        // 释放
        sqlSession.close();
    }

    public Brand selectById(int id) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        // 调用方法
        Brand brand = brandMapper.selectById(id);
        // 释放
        sqlSession.close();
        return brand;
    }
}
